package com.smartao.core;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.dialect.Dialect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.vendor.Database;

import com.smartao.core.info.server.AppServer;
import com.smartao.core.license.License;
import com.smartao.core.orm.DialectUtil;
import com.smartao.extend.Date;

public class BaseContext {
	private static String SID;
	private static String rootPath;
	private static String classPath;
	private static String contextPath;
	private static Database database;
	private static AppServer appServer;
	private static Properties sysProps = System.getProperties();
	private static License license;
	private static Date startTime;

	private static final Logger logger = LoggerFactory.getLogger(BaseContext.class);

	/**
	 * 初始化
	 * @param rootpath
	 * @param contextpath
	 */
	public static void init() {
		logger.info("Initalizing BaseContext...");
		BaseContext.startTime = new Date();
	}

	/**
	 * @param dsName 数据源名称
	 * @return 返回容器中指定名称的数据源
	 */
	public static DataSource getDataSource(String dsName) {
		return (DataSource) appServer.lookup(dsName);
	}

	public static String getContextPath() {
		return contextPath;
	}

	/**
	 * @return WebApp根目录,结尾有分隔符'/'
	 */
	public static String getRootPath() {
		return rootPath;
	}

	/**
	 * @return classes目录  WebApp/WEB-INF/classes/
	 */
	public static String getClassPath() {
		return classPath;
	}

	public static void setClassPath(String classPath) {
		BaseContext.classPath = classPath;
	}

	public static String getSID() {
		return SID;
	}

	public static void setSID(String sID) {
		SID = sID;
	}

	public static Properties getSysProps() {
		return sysProps;
	}

	public static void setSysProps(Properties sysProps) {
		BaseContext.sysProps = sysProps;
	}

	public static License getLicense() {
		return license;
	}

	public static void setLicense(License license) {
		BaseContext.license = license;
	}

	public static Database getDatabase() {
		return database;
	}
	
	public static Dialect getDialect() {
		try {
			return DialectUtil.getDatabaseDialect(database);
		} catch (Exception e) {
			logger.error("Get Dialect Error:",e);
			return null;
		}
	}

	public static void setDatabase(Database database) {
		BaseContext.database = database;
	}

	public static void setRootPath(String rootPath) {
		BaseContext.rootPath = rootPath;
	}

	public static void setContextPath(String contextPath) {
		BaseContext.contextPath = contextPath;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SID:" + SID);
		//buffer.append("Dialect:" + eweaverProps.getProperty(Environment.DIALECT));
		buffer.append("RootPath:" + rootPath);
		buffer.append("ContextPath:" + contextPath);
		buffer.append("Database:" + database);
		return buffer.toString();
	}

	public static AppServer getAppServer() {
		return appServer;
	}

	public static void setAppServer(AppServer appServer) {
		BaseContext.appServer = appServer;
	}

	public static Date getStartTime() {
		return startTime;
	}
}
